<%@ page language="java" pageEncoding="UTF-8"%>
<link href="${pageContext.request.contextPath}/js/jquery/daterangepicker/css/ui.daterangepicker.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery/daterangepicker/js/daterangepicker.jQuery.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery/daterangepicker/js/ui.datepicker-zh-CN.js"></script>

<script type="text/javascript">
<!--//--><![CDATA[//><!--

!function ($) {


$.fn.dt_range_picker = function(setting){
	var local_setting = setting;
	
	var show_today = local_setting.show_today;
	var limit_month = local_setting.limit_month;
	var start_date = local_setting.start_date;
	var end_date = local_setting.end_date;
	var oldStartVal;
	var oldEndVal;
	
	var maxCurrent = Date.parse('today');
	
	var ranges = [
		   	{text: '今天', dateStart: 'today', dateEnd: 'today' },
			{text: '昨天', dateStart: 'yesterday', dateEnd: 'yesterday' },
//			{text: '上周', dateStart: function(){ var x=Date.parse('today'); var num=x.getDay(); if(num == 0){num=7;} var monday  = x.getTime() - (num+6)*24*60*60*1000; var str=new Date(monday).toLocaleDateString();var curYear = str.substring(0,str.indexOf('年'));var curMonth =str.substring(str.indexOf('年')+1,str.indexOf('月'));var curDay =str.substring(str.indexOf('月')+1,str.indexOf('日'));if (curMonth<10){curMonth="0"+curMonth;}if(curDay<10){curDay="0"+curDay;} var time = curYear+"-"+curMonth+"-"+curDay; return Date.parse(time);},dateEnd: function(){ var x=Date.parse('today'); var num=x.getDay(); if(num == 0){num=7;} var monday  = x.getTime() - (num)*24*60*60*1000; var str=new Date(monday).toLocaleDateString();var curYear = str.substring(0,str.indexOf('年'));var curMonth =str.substring(str.indexOf('年')+1,str.indexOf('月'));var curDay =str.substring(str.indexOf('月')+1,str.indexOf('日'));if (curMonth<10){curMonth="0"+curMonth;}if(curDay<10){curDay="0"+curDay;} var time = curYear+"-"+curMonth+"-"+curDay; return Date.parse(time);} },
			{text: '上周', dateStart: function(){var x=Date.parse('today'); var num=x.getDay(); if(num == 0){num=7;} var monday  = x.getTime() - (num+6)*24*60*60*1000; var str=new Date(monday);return str;}, dateEnd: function(){var x=Date.parse('today'); var num=x.getDay(); if(num == 0){num=7;} var monday  = x.getTime() - (num)*24*60*60*1000; var str=new Date(monday); return str;} },
			{text: '最近七天', dateStart: 'today-7days', dateEnd: 'yesterday' },
			{text: '本月', dateStart: function(){ return Date.parse('today').moveToFirstDayOfMonth();  }, dateEnd: 'yesterday' },
			{text: '上个月', dateStart: function(){ return Date.parse('1 month ago').moveToFirstDayOfMonth();  }, dateEnd: function(){ return Date.parse('1 month ago').moveToLastDayOfMonth();  } },
			{text: '本年', dateStart: function(){ var x= Date.parse('today'); x.setMonth(0); x.setDate(1); return x; }, dateEnd: 'yesterday' }
		];
	var presetRanges = new Array();
	//屏蔽“今天”,“本年”选项
		jQuery.each(ranges,function(){
			var hasExclude = false;
			var text = this.text;
			var currentDate = new Date();
			var day = currentDate.getDate();
			//屏蔽当天日期的选择
				if(text == '今天' && !show_today)
				{
					maxCurrent = Date.parse('yesterday');
					hasExclude = true;
				}else if(text == '本月' &&  !show_today){
					//当最大日期为昨天并且当天日期为1号时,屏蔽“本月”选项
					if(day == '1'){
						hasExclude = true;
					}
				}else if(text == '本年' && limit_month){
					hasExclude = true;
				}
			if(!hasExclude){
				presetRanges.push(this);
			}
		});
	
	$(this).daterangepicker({
		presetRanges:presetRanges,
		arrows : true,
		datepickerOptions : {
			changeMonth : true,
			changeYear : true,
			minDate : Date.parse('2012-08-01'),
			maxDate : maxCurrent
		},onOpen:function(){
			oldStartVal = $("#"+start_date).val();
			oldEndVal = $("#"+end_date).val();
		},onChange : function() {
			var star_time = $("#"+start_date).val();
			var end_time = $("#"+end_date).val();
			var start = new  Date(Date.parse(star_time.replace(/-/g,   "/")));   
			var end = new  Date(Date.parse(end_time.replace(/-/g,   "/")));
			if(start > end){
				var tmp = star_time;
				star_time = end_time;
				end_time = tmp;
				$("#"+start_date).val(star_time);
				$("#"+end_date).val(end_time);
				return;
			}
			if(limit_month){
				
		 		app_log( start.toString('yyyy-MM-dd') + ' year: ' + start.getFullYear() + ' month: ' + start.getMonth() );
				app_log( end.toString('yyyy-MM-dd')   + ' year: ' +   end.getFullYear() + ' month: ' +   end.getMonth());
				
				var monthNum = putMonthN(start.toString('yyyy-MM-dd'),end.toString('yyyy-MM-dd'));
				
				if(monthNum >= 3){
					alert('查询时间跨度需在3个自然月内。');
					$('#' + start_date).val( oldStartVal );
	 	 			$('#' + end_date).val( oldEndVal );
				}
				
// 				var isOutOfMonth = false;
// 				if( start.getFullYear() == end.getFullYear() ){

// 					isOutOfMonth = (end.getMonth() - start.getMonth() > 2 ) ? true : false;

// 				}else{
// 					isOutOfMonth = ( (end.getMonth()+12) - start.getMonth() > 2 ) ? true : false;
				
// 				}
			
// 				if( isOutOfMonth ){ 
					
// 					alert('查询时间跨度需在3个自然月内。');
// 					app_log( container.val(oldVal) );
					
// 					$('#' + start_date).val( oldStartVal );
// 	 	 			$('#' + end_date).val( oldEndVal );
	 	 			
// 				}else{
// 					$('#' + start_date).val( start.toString('yyyy-MM-dd') );
// 	 	 			$('#' + end_date).val( end.toString('yyyy-MM-dd') );
// 				}
				
		 
		 	
			}
		},
		presets:{
			dateRange:'自定义时间段'
		}
	});

	
};



} (window.jQuery);


function monthDays(y,m) {

	var solarMonth=new Array(31,28,31,30,31,30,31,31,30,31,30,31);

	   if(m==1)
	      return(((y%4 == 0) && (y%100 != 0) || (y%400 == 0))? 29: 28)
	   else
	      return(solarMonth[m])
	}



	function putMonthN(a,b) 
	{

	var da,db;

	//得到两个日期数组
	da=a.split("-");
	db=b.split("-");

	//得到年差月差日差

	y=da[0]-db[0];
	m=da[1]-db[1];
	d=da[2]-db[2];

	//得知a/b哪个日期更早  a早 标记为-1 b早标记为0 标记为flag

	flag=(y>0 ||(y==0 && m>0) || (y==0 && m==0 && d>0))?0:-1;

	//计算月数
	if(flag==-1)
	{
	y=-y;m=-m;d=-d;
	bigMonthDN=monthDays(db[0],db[1]-1);
	}
	else
	bigMonthDN=monthDays(da[0],da[1]-1);


	num=12*y + m + ((d<bigMonthDN-1)? ( (d>=-1)?0:-1 ):1)

	return num;
	}




//--><!
</script>